﻿/**
 * Copyright (C) 2011 original author and authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NGuice.Inject.Internal.Util;

namespace NGuice.Inject.Spi
{
    //complete!
    /// <summary>
    /// 一个类型成员注入器的lookup。他显式的在模型中使用Binder.GetMembersInjector(Type)创建：
    /// <code>
    /// MembersInjector&lt;PaymentService&gt; membersInjector = GetMembersInjector(typeof(PaymentService));
    /// </code>
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public sealed class MembersInjectorLookup<T> : IElement
    {
        private readonly object source;
        private readonly Type type;
        private IMembersInjector<T> delegatee;

        public MembersInjectorLookup(object source)
        {
            this.source = Preconditions.CheckNotNull(source, "source");
            this.type = Preconditions.CheckNotNull(typeof(T), "type");
        }

        public object Source
        {
            get { return source; }
        }

        /// <summary>
        /// 包含被注入的成员的类型
        /// </summary>
        /// <returns></returns>
        public Type Type
        {
            get
            {
                return type;
            }
        }

        /// <summary>
        /// </summary>
        /// <param name="delegatee"></param>
        public void InitializeDelegate(IMembersInjector<T> delegatee)
        {
            Preconditions.CheckState(this.delegatee == null, "delegate already initialized");
            this.delegatee = Preconditions.CheckNotNull(delegatee, "delegate");
        }

        public T AcceptVisitor<T>(IElementVisitor<T> visitor)
        {
            return visitor.Visit(this);
        }

        public void ApplyTo(IBinder binder)
        {
            InitializeDelegate(binder.WithSource(Source).GetMembersInjector<T>());
        }

        /// <summary>
        /// 返回成员注入器的委托，如果没有初始化则返回null。委托当此元素被处理后初始化否则用于创建注入器
        /// </summary>
        public IMembersInjector<T> Delegatee
        {
            get
            {
                return delegatee;
            }
        }

        /// <summary>
        /// 返回查找的成员注入器。结果直到此lookup被初始化后才有效，他通常在注入器创建后发生。
        /// 如果你在之前使用，成员注入器将抛出IllegalStateException异常
        /// </summary>
        public IMembersInjector<T> MembersInjector
        {
            get
            {
                return new InternalMemberInjector(this);
            }

        }

        private class InternalMemberInjector : IMembersInjector<T>
        {
            private MembersInjectorLookup<T> outer;

            public InternalMemberInjector(MembersInjectorLookup<T> outer)
            {
                this.outer = outer;
            }

            public void InjectMembers(T instance)
            {
                Preconditions.CheckState(outer.delegatee != null, "This MembersInjector cannot be used until the Injector has been created.");
                outer.delegatee.InjectMembers(instance);
            }
            public override string ToString()
            {
                return "MembersInjector<" + outer.type + ">";
            }
        }

    }
}
